/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import scores.HibernateUtil;
import java.util.Date;
import java.util.List;
import javax.faces.context.FacesContext;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author ALIEN
 */
public class Bettor {
    private Session session;
    private boolean loggedIn;
    private boolean loggedInAndAdmin;
    private boolean loggedInAndNotAdmin;
    private boolean admin;
    private long id;
    private String login;
    private String password;
    private String name;
    private String surname;
    private Date birthdate;
    private String email;
    private String address;
    private String city;
    private String postcode;
    private String country;
    private String phone;
    private double balance;

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the login
     */
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    
    /**
     * @return the loggedIn
     */
    public boolean isLoggedIn() {
        return loggedIn;
    }

    /**
     * @param loggedIn the loggedIn to set
     */
    public void setLoggedIn(boolean loggedIn) {
        this.loggedIn = loggedIn;
    }

    /**
     * @return the loggedInAndAdmin
     */
    public boolean isLoggedInAndAdmin() {
        return (loggedIn||admin);
    }

    /**
     * @return the loggedInAndNotAdmin
     */
    public boolean isloggedInAndNotAdmin() {
        return (loggedIn&&!isAdmin());
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the surname
     */
    public String getSurname() {
        return surname;
    }

    /**
     * @param surname the surname to set
     */
    public void setSurname(String surname) {
        this.surname = surname;
    }

    /**
     * @return the birthdate
     */
    public Date getBirthdate() {
        return birthdate;
    }

    /**
     * @param birthdate the birthdate to set
     */
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the postcode
     */
    public String getPostcode() {
        return postcode;
    }

    /**
     * @param postcode the postcode to set
     */
    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    /**
     * @return the country
     */
    public String getCountry() {
        return country;
    }

    /**
     * @param country the country to set
     */
    public void setCountry(String country) {
        this.country = country;
    }

    /**
     * @return the phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * @param phone the phone to set
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * @return the balance
     */
    public double getBalance() {
        return balance;
    }

    /**
     * @param balance the balance to set
     */
    public void setBalance(double balance) {
        this.balance = balance;
    }

    /**
     * @param admin the admin to set
     */
    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public boolean isAdmin() {
        if (this.login != null)
            if (this.login.equals("admin"))
                return true;
        return false;
    }

    public String dodaj() {
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            this.balance = 0;
            session.save(this);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        finally {
            session.close();
        }
        return "ok";
    }

    public String loguj(){
        List l = null;
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            l = session.createQuery("select b from Bettor b where b.login = :login and b.password = :password").setParameter("login", login).setParameter("password",password).list();
            //session.load(this,((Bettor) (l.get(0))).id);
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        finally {
            if(session!=null)
            session.close();
        }
        if(l.size()==0){
            FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("param", "LoginError");
            return "fail";
        }

        this.id = ((Bettor) (l.get(0))).getId();
        this.name = ((Bettor) (l.get(0))).getName();
        this.surname = ((Bettor) (l.get(0))).getSurname();
        this.birthdate = ((Bettor) (l.get(0))).getBirthdate();
        this.email = ((Bettor) (l.get(0))).getEmail();
        this.address = ((Bettor) (l.get(0))).getAddress();
        this.city = ((Bettor) (l.get(0))).getCity();
        this.postcode = ((Bettor) (l.get(0))).getPostcode();
        this.country = ((Bettor) (l.get(0))).getCountry();
        this.phone = ((Bettor) (l.get(0))).getPhone();
        this.balance = ((Bettor) (l.get(0))).getBalance();
        this.setLoggedIn(true);
        return "success";
    }



    public String wyloguj(){
        this.login = null;
        this.setLoggedIn(false);
        return "success";
    }


   public String deposit(double credit){
       this.balance += credit;
       return "success";
    }

   public String withdraw(double credit){
       this.balance -= credit;
       return "success";
    }
}
